<template>
  <div class="complaint-submit">
    <div class="sub-page-header">
      <el-tooltip content="点击返回判定规则" placement="left">
        <div class="title" @click="goBack">
          <el-button icon="el-icon-arrow-left" type="text"></el-button>
          {{ formDisabled ? '规则详情' : id ? '修改判定规则' : '新增判定规则' }}
        </div>
      </el-tooltip>
    </div>
    <el-scrollbar class="apply-box submit-fixed">
      <div class="content">
        <div class="form">
          <el-form
            label-suffix="："
            :model="submitForm"
            :rules="rules"
            label-width="130px"
            ref="dataForm"
            :disabled="formDisabled">
            <div class="section-title syht16">规则详情</div>
            <el-form-item prop="ruleName" label="规则名称">
              <el-input
                placeholder="请输入"
                v-model="submitForm.ruleName"
                maxlength="120"
                show-word-limit
                clearable></el-input>
            </el-form-item>
            <el-form-item label="规则编码">
              <el-input
                placeholder="请输入"
                v-model="submitForm.ruleCode"
                maxlength="50"
                show-word-limit
                clearable></el-input>
            </el-form-item>
            <el-form-item label="规则设置" class="mb0" prop="ruleIndex">
              <div class="rule-set">
                <el-form-item prop="ruleIndex" label-width="0" class="mr10">
                  <el-select v-model="submitForm.ruleIndex" class="mr10" clearable>
                    <el-option
                      v-for="item in ruleIndexList"
                      :key="item.id"
                      :value="item.id"
                      :label="item.name"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item prop="operationType" label-width="0" class="mr10">
                  <el-select v-model="submitForm.operationType" clearable>
                    <el-option
                      v-for="item in connectorList"
                      :key="item.key"
                      :value="item.label"
                      :label="item.label"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item prop="setValue" label-width="0" class="mr15">
                  <el-input-number
                    placeholder="请输入"
                    controls-position="right"
                    v-model="submitForm.setValue"
                    :min="0"></el-input-number>
                  <span class="ml10">{{ unit }}</span>
                </el-form-item>

                <el-select v-model="submitForm.operationTypeNext" class="mr10" clearable>
                  <el-option
                    v-for="item in connectorList"
                    :key="item.key"
                    :value="item.label"
                    :label="item.label"></el-option>
                </el-select>
                <el-form-item label-width="0" class="mr15">
                  <el-input-number
                    placeholder="请输入"
                    controls-position="right"
                    v-model="submitForm.setValueNext"
                    :min="0"></el-input-number>
                  <span class="ml10">{{ unit }}</span>
                </el-form-item>
              </div>
            </el-form-item>
            <el-form-item prop="ruleStatement" label="规则说明">
              <el-input
                placeholder="请输入"
                v-model="submitForm.ruleStatement"
                maxlength="200"
                show-word-limit
                type="textarea"
                rows="3"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="!formDisabled" class="btns-view">
          <div class="btn-box">
            <el-button class="btn-item" type="primary" @click="submitData">提交</el-button>
          </div>
        </div>
      </div>
    </el-scrollbar>
  </div>
</template>

<script>
import { saveRulesApi, getRuleDetailApi } from '@api/modules/judgRule.js'

import { mapActions, mapGetters } from 'vuex'
import { CONNECT_LIST } from '@utility/dict.js'

export default {
  name: 'judgRuleCreate',
  components: {},
  props: {},
  data() {
    return {
      id: '',
      ruleIndexList: [],
      connectorList: CONNECT_LIST._keys.map(key => {
        return {
          key,
          label: CONNECT_LIST._label[key],
        }
      }),
      submitForm: {
        ruleName: '',
        ruleCode: '',
        ruleIndex: '',
        operationType: '',
        setValue: undefined,
        operationTypeNext: '',
        setValueNext: undefined,
        ruleStatement: '',
      },
      rules: {
        ruleName: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
        ruleIndex: [{ required: true, message: '请选择规则设置', trigger: 'blur' }],
        operationType: [{ required: true, message: '请选择条件', trigger: 'blur' }],
        setValue: [{ required: true, message: '请输入数值', trigger: 'blur' }],
      },
    }
  },
  computed: {
    ...mapGetters('publicCode', ['getCode']),
    formDisabled() {
      return this.$route.params.view
    },
    unit() {
      return this.submitForm.ruleIndex ? this.ruleIndexList.find(e => e.id === this.submitForm.ruleIndex).remark : ''
    },
  },
  created() {
    this.getDictData()
    const { id } = this.$route.params
    this.id = id
    if (this.id) {
      this.getDetailData()
    }
  },
  mounted() {},
  methods: {
    ...mapActions('publicCode', ['getListDictData']),
    getDetailData() {
      getRuleDetailApi({ id: this.id }).then(res => {
        if (res.code === 0) {
          this.submitForm = { ...this.submitForm, ...res.data }
          if (!this.submitForm.setValue) {
            this.submitForm.setValue = undefined
          }
          if (!this.submitForm.setValueNext) {
            this.submitForm.setValueNext = undefined
          }
        }
      })
    },
    submitData() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.submitForm.operationTypeNext && !this.submitForm.setValueNext) {
            this.$message.warning('规则设置数值缺少，请完善！')
            return
          }
          const params = { ...this.submitForm }
          saveRulesApi(params).then(res => {
            if (res.code === 0) {
              this.$message.success(res.data)
              this.goBack()
            }
          })
        }
      })
    },
    // 重置表单
    resetForm() {
      if (this.$refs.dataForm) {
        this.$refs.dataForm.resetFields()
      }
    },
    async getDictData() {
      // 规则设置
      await this.getListDictData({ param: 'RULE_INDEX' })

      const { RULE_INDEX } = this.getCode
      this.ruleIndexList = [...RULE_INDEX]
    },
    goBack() {
      this.$router.back(-1)
    },
  },
}
</script>

<style lang="scss" scoped>
.complaint-submit {
  height: 100%;
  @include flex-y;
  position: relative;
  background: #f5f6fa;
  padding: 20px;
  .el-scrollbar {
    @include flex-1;
  }
  ::v-deep .el-select-group {
    display: flex;
    flex-wrap: wrap;
  }

  .apply-box {
    background-color: #fff;
    .content {
      padding: 20px 30px;
      // width: 75%;
    }
    .plus {
      cursor: pointer;
      font-size: 22px;
      margin-left: 10px;
    }
    .rules-select {
      display: flex;
    }
    .rule-oper {
      margin-right: 10px;
    }
    .section-title {
      font-weight: bold;
      padding: 10px;
      background: #f0f4f9;
      margin-bottom: 15px;
    }
    ::v-deep .form {
      .el-form-item {
        margin-bottom: 22px;
      }
    }
  }
  .rule-set {
    display: flex;
  }
  .btns-view {
    // @include flex-x(center, center);
    text-align: center;
    // border-top: 1px solid $-border-color-base;
    margin-top: 10px;
    padding: 20px;
    background-color: #fff;
    .other-btn-box {
      margin-top: 10px;
      text-align: center;
    }
    .btn-item {
      width: 120px;
      height: 40px;
      font-size: 16px;
    }
    .empty {
      background-color: transparent;
      color: #4a79ba;
    }
  }
  .submit-fixed {
    padding-bottom: 100px;
    .btns-view {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
  }

  @media screen and(max-width:800px) {
    .apply-box {
      .content {
        padding: 20px;
      }
    }
    .msg-container {
      .section-title {
        padding: 20px 50px;
        font-size: 36px;
        .icon-img-ts,
        .icon-img-person {
          display: inline-block;
          height: 36px;
          vertical-align: middle;
          margin-right: 10px;
          margin-top: -5px;
        }
      }
    }
  }
}
</style>
